package com.srwl.mytx.greendao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.srwl.mytx.domain.User;

import com.srwl.mytx.domain.Contact;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "CONTACT".
*/
public class ContactDao extends AbstractDao<Contact, String> {

    public static final String TABLENAME = "CONTACT";

    /**
     * Properties of entity Contact.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property CId = new Property(0, String.class, "cId", true, "C_ID");
        public final static Property TId = new Property(1, String.class, "tId", false, "T_ID");
        public final static Property MId = new Property(2, String.class, "mId", false, "M_ID");
        public final static Property Status = new Property(3, String.class, "status", false, "STATUS");
        public final static Property Type = new Property(4, Integer.class, "type", false, "TYPE");
        public final static Property InviteMessage = new Property(5, String.class, "inviteMessage", false, "INVITE_MESSAGE");
        public final static Property GroupName = new Property(6, String.class, "groupName", false, "GROUP_NAME");
        public final static Property Trace = new Property(7, String.class, "trace", false, "TRACE");
        public final static Property Location = new Property(8, String.class, "location", false, "LOCATION");
        public final static Property RelationInfinite = new Property(9, String.class, "relationInfinite", false, "RELATION_INFINITE");
        public final static Property TTrace = new Property(10, String.class, "tTrace", false, "T_TRACE");
        public final static Property TLocation = new Property(11, String.class, "tLocation", false, "T_LOCATION");
        public final static Property TRelationInfinite = new Property(12, String.class, "tRelationInfinite", false, "T_RELATION_INFINITE");
        public final static Property InfiniteTag = new Property(13, String.class, "infiniteTag", false, "INFINITE_TAG");
        public final static Property InfiniteKey = new Property(14, String.class, "infiniteKey", false, "INFINITE_KEY");
        public final static Property InfiniteChat = new Property(15, String.class, "infiniteChat", false, "INFINITE_CHAT");
        public final static Property UpChat = new Property(16, String.class, "upChat", false, "UP_CHAT");
        public final static Property AdjustStructure = new Property(17, String.class, "adjustStructure", false, "ADJUST_STRUCTURE");
        public final static Property Position = new Property(18, int.class, "position", false, "POSITION");
    }

    private DaoSession daoSession;


    public ContactDao(DaoConfig config) {
        super(config);
    }
    
    public ContactDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"CONTACT\" (" + //
                "\"C_ID\" TEXT PRIMARY KEY NOT NULL ," + // 0: cId
                "\"T_ID\" TEXT," + // 1: tId
                "\"M_ID\" TEXT," + // 2: mId
                "\"STATUS\" TEXT," + // 3: status
                "\"TYPE\" INTEGER," + // 4: type
                "\"INVITE_MESSAGE\" TEXT," + // 5: inviteMessage
                "\"GROUP_NAME\" TEXT," + // 6: groupName
                "\"TRACE\" TEXT," + // 7: trace
                "\"LOCATION\" TEXT," + // 8: location
                "\"RELATION_INFINITE\" TEXT," + // 9: relationInfinite
                "\"T_TRACE\" TEXT," + // 10: tTrace
                "\"T_LOCATION\" TEXT," + // 11: tLocation
                "\"T_RELATION_INFINITE\" TEXT," + // 12: tRelationInfinite
                "\"INFINITE_TAG\" TEXT," + // 13: infiniteTag
                "\"INFINITE_KEY\" TEXT," + // 14: infiniteKey
                "\"INFINITE_CHAT\" TEXT," + // 15: infiniteChat
                "\"UP_CHAT\" TEXT," + // 16: upChat
                "\"ADJUST_STRUCTURE\" TEXT," + // 17: adjustStructure
                "\"POSITION\" INTEGER NOT NULL );"); // 18: position
        // Add Indexes
        db.execSQL("CREATE UNIQUE INDEX " + constraint + "IDX_CONTACT_T_ID ON \"CONTACT\"" +
                " (\"T_ID\" ASC);");
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CONTACT\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Contact entity) {
        stmt.clearBindings();
 
        String cId = entity.getCId();
        if (cId != null) {
            stmt.bindString(1, cId);
        }
 
        String tId = entity.getTId();
        if (tId != null) {
            stmt.bindString(2, tId);
        }
 
        String mId = entity.getMId();
        if (mId != null) {
            stmt.bindString(3, mId);
        }
 
        String status = entity.getStatus();
        if (status != null) {
            stmt.bindString(4, status);
        }
 
        Integer type = entity.getType();
        if (type != null) {
            stmt.bindLong(5, type);
        }
 
        String inviteMessage = entity.getInviteMessage();
        if (inviteMessage != null) {
            stmt.bindString(6, inviteMessage);
        }
 
        String groupName = entity.getGroupName();
        if (groupName != null) {
            stmt.bindString(7, groupName);
        }
 
        String trace = entity.getTrace();
        if (trace != null) {
            stmt.bindString(8, trace);
        }
 
        String location = entity.getLocation();
        if (location != null) {
            stmt.bindString(9, location);
        }
 
        String relationInfinite = entity.getRelationInfinite();
        if (relationInfinite != null) {
            stmt.bindString(10, relationInfinite);
        }
 
        String tTrace = entity.getTTrace();
        if (tTrace != null) {
            stmt.bindString(11, tTrace);
        }
 
        String tLocation = entity.getTLocation();
        if (tLocation != null) {
            stmt.bindString(12, tLocation);
        }
 
        String tRelationInfinite = entity.getTRelationInfinite();
        if (tRelationInfinite != null) {
            stmt.bindString(13, tRelationInfinite);
        }
 
        String infiniteTag = entity.getInfiniteTag();
        if (infiniteTag != null) {
            stmt.bindString(14, infiniteTag);
        }
 
        String infiniteKey = entity.getInfiniteKey();
        if (infiniteKey != null) {
            stmt.bindString(15, infiniteKey);
        }
 
        String infiniteChat = entity.getInfiniteChat();
        if (infiniteChat != null) {
            stmt.bindString(16, infiniteChat);
        }
 
        String upChat = entity.getUpChat();
        if (upChat != null) {
            stmt.bindString(17, upChat);
        }
 
        String adjustStructure = entity.getAdjustStructure();
        if (adjustStructure != null) {
            stmt.bindString(18, adjustStructure);
        }
        stmt.bindLong(19, entity.getPosition());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Contact entity) {
        stmt.clearBindings();
 
        String cId = entity.getCId();
        if (cId != null) {
            stmt.bindString(1, cId);
        }
 
        String tId = entity.getTId();
        if (tId != null) {
            stmt.bindString(2, tId);
        }
 
        String mId = entity.getMId();
        if (mId != null) {
            stmt.bindString(3, mId);
        }
 
        String status = entity.getStatus();
        if (status != null) {
            stmt.bindString(4, status);
        }
 
        Integer type = entity.getType();
        if (type != null) {
            stmt.bindLong(5, type);
        }
 
        String inviteMessage = entity.getInviteMessage();
        if (inviteMessage != null) {
            stmt.bindString(6, inviteMessage);
        }
 
        String groupName = entity.getGroupName();
        if (groupName != null) {
            stmt.bindString(7, groupName);
        }
 
        String trace = entity.getTrace();
        if (trace != null) {
            stmt.bindString(8, trace);
        }
 
        String location = entity.getLocation();
        if (location != null) {
            stmt.bindString(9, location);
        }
 
        String relationInfinite = entity.getRelationInfinite();
        if (relationInfinite != null) {
            stmt.bindString(10, relationInfinite);
        }
 
        String tTrace = entity.getTTrace();
        if (tTrace != null) {
            stmt.bindString(11, tTrace);
        }
 
        String tLocation = entity.getTLocation();
        if (tLocation != null) {
            stmt.bindString(12, tLocation);
        }
 
        String tRelationInfinite = entity.getTRelationInfinite();
        if (tRelationInfinite != null) {
            stmt.bindString(13, tRelationInfinite);
        }
 
        String infiniteTag = entity.getInfiniteTag();
        if (infiniteTag != null) {
            stmt.bindString(14, infiniteTag);
        }
 
        String infiniteKey = entity.getInfiniteKey();
        if (infiniteKey != null) {
            stmt.bindString(15, infiniteKey);
        }
 
        String infiniteChat = entity.getInfiniteChat();
        if (infiniteChat != null) {
            stmt.bindString(16, infiniteChat);
        }
 
        String upChat = entity.getUpChat();
        if (upChat != null) {
            stmt.bindString(17, upChat);
        }
 
        String adjustStructure = entity.getAdjustStructure();
        if (adjustStructure != null) {
            stmt.bindString(18, adjustStructure);
        }
        stmt.bindLong(19, entity.getPosition());
    }

    @Override
    protected final void attachEntity(Contact entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
    }    

    @Override
    public Contact readEntity(Cursor cursor, int offset) {
        Contact entity = new Contact( //
            cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // cId
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // tId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // mId
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // status
            cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4), // type
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // inviteMessage
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // groupName
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // trace
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // location
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // relationInfinite
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10), // tTrace
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11), // tLocation
            cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12), // tRelationInfinite
            cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13), // infiniteTag
            cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14), // infiniteKey
            cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15), // infiniteChat
            cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16), // upChat
            cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17), // adjustStructure
            cursor.getInt(offset + 18) // position
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Contact entity, int offset) {
        entity.setCId(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
        entity.setTId(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setMId(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setStatus(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setType(cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4));
        entity.setInviteMessage(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setGroupName(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setTrace(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setLocation(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setRelationInfinite(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setTTrace(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
        entity.setTLocation(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
        entity.setTRelationInfinite(cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12));
        entity.setInfiniteTag(cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13));
        entity.setInfiniteKey(cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14));
        entity.setInfiniteChat(cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15));
        entity.setUpChat(cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16));
        entity.setAdjustStructure(cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17));
        entity.setPosition(cursor.getInt(offset + 18));
     }
    
    @Override
    protected final String updateKeyAfterInsert(Contact entity, long rowId) {
        return entity.getCId();
    }
    
    @Override
    public String getKey(Contact entity) {
        if(entity != null) {
            return entity.getCId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Contact entity) {
        return entity.getCId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getUserDao().getAllColumns());
            builder.append(" FROM CONTACT T");
            builder.append(" LEFT JOIN USER T0 ON T.\"T_ID\"=T0.\"U_ID\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Contact loadCurrentDeep(Cursor cursor, boolean lock) {
        Contact entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        User targetUser = loadCurrentOther(daoSession.getUserDao(), cursor, offset);
        entity.setTargetUser(targetUser);

        return entity;    
    }

    public Contact loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Contact> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Contact> list = new ArrayList<Contact>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Contact> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Contact> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
